package com.dbgs.insurance.insuredperson.fragments;

import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.dbgs.insurance.insuredperson.R;
import com.dbgs.insurance.insuredperson.activity.LoginActivity;
import com.dbgs.insurance.insuredperson.adapter.GridHomeNavigationAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.hss.base.BaseFragment;
import com.lz.commonlibrary.widget.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/4/18.
 */

public class HomeFragment extends BaseFragment {


    @BindView(R.id.map)
    AppCompatImageView map;
    @BindView(R.id.address_tv)
    AppCompatTextView addressTv;
    @BindView(R.id.btn_set)
    AppCompatImageView btnSet;
    @BindView(R.id.btn_message)
    AppCompatImageView btnMessage;
    @BindView(R.id.nsgv_module)
    NoScrollGridView nsgvModule;
    @BindView(R.id.one_line)
    View oneLine;
    @BindView(R.id.name)
    AppCompatTextView name;
    @BindView(R.id.name_tv)
    AppCompatEditText nameTv;
    @BindView(R.id.sex)
    AppCompatTextView sex;
    @BindView(R.id.btn_man)
    RadioButton btnMan;
    @BindView(R.id.btn_woman)
    RadioButton btnWoman;
    @BindView(R.id.sex_rg)
    RadioGroup sexRg;
    @BindView(R.id.age)
    AppCompatTextView age;
    @BindView(R.id.social_security_card_number)
    AppCompatTextView socialSecurityCardNumber;
    @BindView(R.id.social_security_card_number_tv)
    AppCompatEditText socialSecurityCardNumberTv;
    @BindView(R.id.id_num)
    AppCompatTextView idNum;
    @BindView(R.id.id_num_tv)
    AppCompatEditText idNumTv;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.refreshlv)
    PullToRefreshScrollView refreshlv;
    private View root_view;
    private List<String> dataList;
    private GridHomeNavigationAdapter mGridHomeNavigationAdapter;
    Unbinder unbinder;

    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();
    public String mCity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        if (root_view == null) {
            root_view = inflater.inflate(R.layout.fragment_home, container,
                    false);
        }
        ViewGroup parent = (ViewGroup) root_view.getParent();
        if (parent != null) {
            parent.removeView(root_view);
        }

        unbinder = ButterKnife.bind(this, root_view);
        return root_view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);
        startLocate();
        initViews();
        initListener();
    }

    public void initData() {
        dataList = new ArrayList<String>();
        dataList.add("资讯");
        dataList.add("评估");
        dataList.add("服务");
        dataList.add("会员");
    }


    @Override
    public void initViews() {
        super.initViews();
        initData();
        mGridHomeNavigationAdapter = new GridHomeNavigationAdapter(getActivity(), dataList);
        nsgvModule.setAdapter(mGridHomeNavigationAdapter);

        LocationClientOption option = new LocationClientOption();

        option.setIsNeedAddress(true);
        //可选，是否需要地址信息，默认为不需要，即参数为false
        //如果开发者需要获得当前点的地址信息，此处必须为true

        mLocationClient.setLocOption(option);
        //mLocationClient为第二步初始化过的LocationClient对象
        //需将配置好的LocationClientOption对象，通过setLocOption方法传递给LocationClient对象使用
        //更多LocationClientOption的配置，请参照类参考中LocationClientOption类的详细说明
    }

    @OnClick({R.id.next_step_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.next_step_btn:
                startActy(LoginActivity.class);
                break;
        }
    }

    @Override
    public void initListener() {
        super.initListener();
        sexRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                String result = radioButton.getText().toString();
                showToast(result);
            }
        });

    }

    @Override
    public void showMSVLoading() {

    }

    @Override
    public void showMSVEmpty() {

    }

    @Override
    public void showMSVNetErr() {

    }

    @Override
    public void showMSVUnknowErr() {

    }

    @Override
    public void showMSVContent() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 定位
     */
    private void startLocate() {
        mLocationClient = new LocationClient(getActivity().getApplicationContext());     //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);    //注册监听函数
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span = 2000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤GPS仿真结果，默认需要
        mLocationClient.setLocOption(option);
        //开启定位
        mLocationClient.start();
    }

    private class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            StringBuffer sb = new StringBuffer(256);
            if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
                sb.append("gps定位成功");
                addressTv.setText(location.getCity() == null ? "" : location.getCity());
                //addressTv.setText(location.getCity() == null ? "" : location.getCity());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
                addressTv.setText(location.getCity() == null ? "" : location.getCity());
                sb.append("网络定位成功");
            } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
                addressTv.setText(location.getCity() == null ? "" : location.getCity());
                sb.append("离线定位成功，离线定位结果也是有效的");
            } else if (location.getLocType() == BDLocation.TypeServerError) {
                addressTv.setText(location.getCity() == null ? "" : location.getCity());
                sb.append("服务端网络定位失败，可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com，会有人追查原因");
            } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
                addressTv.setText(location.getCity() == null ? "" : location.getCity());
                sb.append("网络不同导致定位失败，请检查网络是否通畅");
            } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
                addressTv.setText(location.getCity() == null ? "" : location.getCity());
                sb.append("无法获取有效定位依据导致定位失败，一般是由于手机的原因，处于飞行模式下一般会造成这种结果，可以试着重启手机");
            }
            mCity = location.getCity();
           // addressTv.setText(mCity == null ? "" : mCity);
            Log.e("描述：", sb.toString());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mLocationClient.start();
       // addressTv.setText(mCity == null ? "" : mCity);
    }
}
